let BASE_URL = '';
let websockUrl = '';
if (process.env.NODE_ENV === 'development') {
			// BASE_URL = 'https://family-care-api.ganorglobal.com';
	BASE_URL = 'http://family-care-api-dev.ganorglobal.com';
	websockUrl='ws://chatroom-dev.ganorglobal.com'
	// BASE_URL = 'http://192.168.50.3:8090'; // 魏
} else {
	BASE_URL = 'https://family-care-api.ganorglobal.com';
	// BASE_URL = 'http://family-care-api-dev.ganorglobal.com';
	websockUrl = 'wss://chatroom.ganorglobal.com'
	// BASE_URL = 'http://app-api-dev.ganorglobal.com';
}
var CryptoJS = require('crypto-js')
const http = {
	api: BASE_URL,
	// api:"https://app-api.ganorglobal.com",
	// api: 'http://192.168.5.147:9501',
	// api: 'http://10.66.108.48:9501',
	request: function(url, method, data, success, isAuthorization = true) {
		this.network(url, method, data, success, isAuthorization)
	},
	network: function(url, method, data, success, isAuthorization) {
		// let Authorization = this.getToken();
		// let CustomerId = this.getCustomerId();
		// let SchoolId = this.getSchoolId();
		// console.log(this)
		let headers = {
			'app-id': uni.getStorageSync('app_id')
		}

		let Authorization = this.getToken();
		let TstampDate = this.getTstampDate();
		data['token'] = Authorization
		data['tstamp'] = TstampDate
		let SignInfo = this.getSignInfo(data);
		data['sign'] = SignInfo

		// if (isAuthorization) {
		// 	headers['token'] = this.getToken();
		// 	headers['customer-id'] = this.getCustomerId();
		// 	headers['school-id'] = this.getSchoolId();
		// 	headers['company-id'] = this.getCompanyId();
			
		// }
		try {
			// console.log(url + '-域名:',this.api);
			// console.log(url + '-头部:',headers);
			// console.log(url + '-方式:',method);
			// console.log(url + '-参数:',data);
			uni.request({
				url: this.api + '/pt' + url ,
				method: method,
				data: data,
				header: headers,
				sslVerify: false,
				success: (result) => {
					// console.log('接口返回数据',result)
					// console.log(url + '-响应:',result);
					uni.hideLoading();
					let pass = true
					if (result.statusCode !== 200) {
						switch (result.statusCode) {
							case 400:
								result.message = '请求错误(400)';
								break;
							case 401:
								result.message = '未授权，请重新登录(401)';
								break;
							case 403:
								result.message = '拒绝访问(403)';
								break;
							case 404:
								result.message = '请求出错(404)';
								break;
							case 405:
								result.message = '请求方法出错(405)';
								break;
							case 408:
								result.message = '请求超时(408)';
								break;
							case 500:
								result.message = '服务器错误(500)';
								break;
							case 501:
								result.message = '服务未实现(501)';
								break;
							case 502:
								result.message = '网络错误(502)';
								break;
							case 503:
								result.message = '服务不可用(503)';
								break;
							case 504:
								result.message = '网络超时(504)';
								break;
							case 505:
								result.message = 'HTTP版本不受支持(505)';
								break;
							default:
								result.message = `连接出错!`;
						}
						uni.showToast({
							title: result.message,
							icon: 'none',
							duration: 3000
						})
						return
					}
					if (result.data.code !== 0) {
						const msgType = typeof result.data.message === Object
						// console.log("msgType", msgType)
						const errorMessage = Object.values(result.data.message).join(',').split(',')
							.join('<br/>')
						if (msgType) {
							uni.showToast({
								icon: "none",
								title: errorMessage,
								duration: 3000
							})
						} else {

							// console.log(result.data)
							uni.showToast({
								icon: "none",
								title: result.data.message,
								duration: 3000
							})
						}
					}
					if (pass) {
						success(result);
					}
				},
				fail: (err) => {
					uni.showToast({
						title: "请求失败,请稍后重试",
						icon: 'none',
					})
					uni.hideLoading();
				}
			})
		} catch (e) {
			console.log('request', e)
		}

	},
	// 加密签名
	getSignInfo: function(params) {
	
		function setArraySign(params)
		{
		    let tmp = '';
		    let newKey = Object.keys(params).sort();
	
		    for(var i=0;i<newKey.length;i++)
		    {
		        let k = newKey[i];
		        let v = params[k];
		        if(typeof v == 'object')
		        {
		            let tmp2 = setArraySign(v);
		            tmp += tmp2;
		        }
		         else
		        {
		            tmp += k + "=" + v + "&";
		        }
		    }
		    return tmp;
		}
	
	
	
		function haha(params)
		{
		    let str = '';
		    let newKey = Object.keys(params).sort();
		    for(var i=0;i<newKey.length;i++)
		    {
		        let k = newKey[i];
		        let v = params[k];
	
		        if(typeof v == 'object')
		        {
		            let tmp2 = setArraySign(v);
	
		            tmp2 = tmp2.substring(0, tmp2.length - 1);
		            str += k + "=[" + tmp2 + "]&";
		        }
		         else
		        {
		            str += k + "=" + v + "&";
		        }
	
		    }
		    return str;
		}
	
	
		let str = haha(params);
		str = str.substring(0, str.length - 1);
	
		let secretKey = 'JHY**^^Y(KM123nHH';
		let sign = secretKey + str + secretKey;
	
		return CryptoJS.MD5(sign).toString().toUpperCase();
	},
	getTstampDate: function() {
		var time = Date.now()
		return parseInt(time/1000);
	},
	getToken: function() {
		var accessToken = getApp().globalData.access_token; // || uni.getStorageSync('access_token');
		return accessToken;
	},
	getCustomerId: function() {
		if (getApp().globalData.currentCustomer && getApp().globalData.currentCustomer.customer_id) {
			var customer_id = getApp().globalData.currentCustomer.customer_id || 0
			// console.log('customer_id',customer_id)
			return customer_id;
		}
		return 0;
	},
	getSchoolId: function() {
		if (getApp().globalData.currentCustomer && getApp().globalData.currentCustomer.school_id) {
			var school_id = getApp().globalData.currentCustomer.school_id || 0
			// console.log('school_id',school_id)
			return school_id;
		}
		return 0;
	},
	getCompanyId: function() {
		if (getApp().globalData.currentCustomer && getApp().globalData.currentCustomer.school_id) {
			var company_id = getApp().globalData.currentCustomer.company_id || 0
			// console.log('company_id',company_id)
			return company_id;
		}
		return 0;
	},

};
export default {
	http,
	websockUrl
}
